package com.forum.core.action;

import com.forum.core.dao.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


/**
 * @Company <Jinher Technology Development Company LTD.>
 * @Project <Jinher Collaboration Suite>
 * @version <1.0>
 * @Author  <yangzq>
 * @Date    <May 18, 2009>
 * @description 
 * 
 * Struts2中典型CRUD Action的抽象基类.
 * 
 * 主要定义了对Preparable,ModelDriven接口的使用,以及CRUD函数和返回值的命名.
 *
 * @param <T> CRUDAction所管理的对象类型. 
 */



@SuppressWarnings({ "serial", "unchecked" })
public abstract class CRUDActionSupport<T> extends BaseAction implements ModelDriven<T>, Preparable{
	/**
	 * 进行增删改操作后,以redirect方式重新打开action默认页的result名.
	 */
	public static final String RELOAD = "reload";
	public static final String DETAIL = "detail";
	public static final String EDIT = "edit";
	public Map<String,Object> session ;
	protected final Logger logger = LoggerFactory.getLogger(getClass());
    public Page<T> page;


	/**
	 * Action函数,默认action函数，默认指向list函数.
	 */
	@Override
	public String execute() throws Exception {
		return list();
	}

	// CRUD函数 //

	/**
	 * Action函数,显示Entity列表.
	 * 建议return SUCCESS.
	 */
	public abstract String list() throws Exception;

	/**
	 * Action函数,新增或修改Entity. 
	 * 建议return RELOAD.
	 */
	public abstract String save() throws Exception;

	/**
	 * Action函数,删除Entity.
	 * 建议return RELOAD.
	 */
	public abstract String delete() throws Exception;
	

	// Preparable函数 //

	/**
	 * 实现空的prepare()函数,屏蔽所有Action函数公共的二次绑定.
	 */
	public abstract void prepare() throws Exception;

	/**
	 * 在save()前执行二次绑定.
	 */
	public void prepareSave() throws Exception {
		prepareModel();
	}

	/**
	 * 在input()前执行二次绑定.
	 */
	public void prepareInput() throws Exception {
		prepareModel();
	}
	
	/**
	 * 等同于prepare()的内部函数,供prepardMethodName()函数调用. 
	 */
	protected  void prepareModel() throws Exception{
		
	}
	
	/**
	 * 若要在页面访问page的内容，此类的实现类必须实现这个方法 
	 */
	public abstract Page<T> getPage() throws Exception;

	
}
